﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Collisions;
using UnitLibrary.GameObjects.Physics;

namespace UnitLibrary.GameObjects.Units
{    
    public class PowerUp: PhyObj
    {
        public PowerUp()
            : base()
        { Name = "HealthPack"; }
        /// <summary>
        /// How long the power-up will remain in the game
        /// </summary>
        private double _durationOnField = 500;
       
        /// <summary>
        /// How long the power-up will stay in the game
        /// </summary>
        public double DurationOnField
        { 
            get {return _durationOnField; } 
            set { if (value > 0) _durationOnField = value; } 
        }

        public string Name
        { get; set; }

        private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList)
        {
            Iid g1tag = (Iid)geom1.Tag;
            Iid g2tag = (Iid)geom2.Tag;
            //Geom 2 tag was never created, we don't know the type, so just return true

            if (geom1.Tag == null)
            {
                Console.WriteLine("Geom 1 tag inside of Enemy.cs OnCollision was never instantiated");
                //Return true since we don't know what the geoms are
                return true;
            }
            if (geom2.Tag == null)
            {
                Console.WriteLine("Geom 2 tag inside of Enemy.cs OnCollision was never instantiated");
                //Return true since we don't know what the geoms are
                return true;
            }
            //Only remove itself it it collides with the player
            if (g1tag.UnitID == UnitID.PowerUp)
            {
                if (g2tag.UnitID == UnitID.Player)
                    _components.RemoveUnit(this.ID, false);
            }
            return false;
        }

        public override void Create(object genericUnit, UnitLibrary.Managers.ComponentM components, Vector2 location, Vector2 direction, bool addtophysics)
        {
            base.Create(genericUnit, components, location, direction, addtophysics);
            if (base._geom.OnCollision == null)
                base._geom.OnCollision += this.OnCollision;
        }

        public override void Update(GameTime gametime)
        {
            _durationOnField -= gametime.ElapsedGameTime.TotalMilliseconds; 
            if (_durationOnField < 0)
                _components.RemoveUnit(this.ID, false);
            base.Update(gametime);
        }
    }
}
